<?php
require 'inc_security.php';
$bil_status = array(
    0 => 'Chưa thanh toán',
    1 => 'Đã thanh toán',
    2 => 'Đon hủy'
);
$list = new dataGrid('bil_id', 30);
$list->add('bil_code', 'Mã hóa đơn', 'string', 1, 1, 'width="120px"');
$list->add('', 'SP đặt mua', 'string');
$list->add('', 'Thời gian', 'string');
$list->add('', 'Giá trị đơn hàng', 'string');
$list->add('bil_status', 'Trạng thái', 'array', 1, 0);
$list->add('', 'Người đặt');
$list->add('', 'Duyệt TT');
$list->add('', 'Hủy đơn');
$db_count = new db_count('SELECT count(*) as count 
                            FROM ' . $bg_table . '
                            WHERE 1 
                            ');
$total = $db_count->total;
unset($db_count);

$db_listing = new db_query('SELECT * 
                            FROM ' . $bg_table . '
                            WHERE 1 ' . $list->sqlSearch() . '
                            ORDER BY ' . $list->sqlSort() . ' bil_id DESC
                            ' . $list->limit($total));
$total_row = mysql_num_rows($db_listing->result);
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="vi" lang="vi" xmlns:og="http://ogp.me/ns#"
      xmlns:fb="https://www.facebook.com/2008/fbml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <?= $load_header ?>
</head>
<body>
<div class="module_header bold fix"><?= $module_name ?></div>
<div id="wrapper">
    <?= $list->showHeader($total_row) ?>
    <?php
    $i = 0;
    ?>
    <?php while ($row = mysql_fetch_assoc($db_listing->result)) {
        $i++;
        ?>
        <?= $list->start_tr($i, $row[$id_field]) ?>
        <td class="center">
            <?= $row['bil_code'] ?>
        </td>
        <td class="center">
            <?
            $products = json_decode(base64_decode($row['bil_product_number_info']), 1);
            $j = 0;
            foreach ($products as $id => $info) {
                $j++;
                $array_url = array('fac_name' => $info['fac_name'], 'pro_id' => $id, 'pro_name' => $info['name']);
                ?>
                <div>
                    <?= $j . '- ' ?><a href="<?= generate_product_detail_url($array_url) ?>"><?= $info['name'] ?></a>
                    - SL: <?= $info['number'] ?> cái
                    - ĐG: <?= format_price($info['price']) ?>
                    - (còn <?= Product::getNumberInstock($id) ?> sp trong kho)
                </div>
            <? } ?>
        </td>
        <td class="center"><?= date('H\hi \n\g\à\y d/m', $row['bil_date']) ?></td>
        <td>
            <label>Chưa phí vận chuyển : <?= format_price($row['bil_money_before_transport_fee']) ?></label>
            <label>Bao gồm phí vận chuyển : <?= format_price($row['bil_money_after_transport_fee']) ?></label>
        </td>
        <td class="center">
            <?= $bil_status[$row['bil_status']] ?>
        </td>
        <td class="center">
            <a href="<?= call_module_file('bills', 'viewdetail') ?>?uid=<?= $row['bil_user_id'] ?>">View</a>
        </td>
        <td class="center">
            <?if ($row['bil_status'] == 1) {
                $log_bill = Bill::getAdminActiveBill($row[$id_field]);
                echo 'Được duyệt bởi <b>' . $log_bill['adm_loginname'] . '</b> lúc ' . date('H:ia d/m/y', $log_bill['log_time']);

            } else {
                ?>
                <a href="success.php?id=<?= $row[$id_field] ?>">Duyệt TT</a>
            <? } ?>
        </td>
        <td class="center">
            <?if ($row['bil_status'] == 2) {
                $log_bill = Bill::getAdminActiveBill($row[$id_field]);
                echo 'Đã hủy bởi <b>' . $log_bill['adm_loginname'] . '</b> lúc ' . date('H:ia d/m/y', $log_bill['log_time']);
            } else {
                ?>
                <a href="cancel.php?id=<?= $row[$id_field] ?>">Hủy</a>
            <? } ?>
        </td>
        <?= $list->end_tr() ?>
    <? } ?>
    <?= $list->showFooter() ?>
</div>
</body>
</html>